<?php
require_once 'pages/TMSFrontListPage.inc';

class TMSFrontSearchResultsPage extends TMSFrontListPage {
	protected $searchCondition = null;
	
	public function Handle(THttpRequest $request){
		if (isset($request->search))
			$this->searchCondition = $request->search;
		parent::Handle($request);
	}
	
	protected function getFetchOptions(){
		$lo = parent::getFetchOptions();
		if ($this->searchCondition){
			$lo->Filter = new TCondition(TConditionType::C_OR, array(
				new TFilterOption('Title', TConditionType::C_LIKE, $this->searchCondition),
				new TFilterOption('Snippet', TConditionType::C_LIKE, $this->searchCondition),
				new TFilterOption('Keywords', TConditionType::C_LIKE, $this->searchCondition),
				new TFilterOption('Description', TConditionType::C_LIKE, $this->searchCondition),
				new TFilterOption('PageContents', TConditionType::C_LIKE, $this->searchCondition)
			));
			$lo->Filter = new TFilterOption('IsPublished', TConditionType::C_EQUAL, true);
			$lo->Filter = new TFilterOption('Language', TConditionType::C_EQUAL, $this->Application()->Language);
		}
		return $lo;
	}
	
	protected function processUrlParameters(array $parameters){
		$parameters = parent::processUrlParameters($parameters);
		if (!isset($parameters['search']))
			$parameters['search'] = $this->searchCondition;
		return $parameters;
	}	
	
	public function Pages(){
		if ($this->searchCondition)
			return new TMSFrontPageItemIterator($this->PagesModel->GetList($this->getFetchOptions(),$this->totalPageCount), $this);
		return new TArrayIterator(array());	
	}
}